//
//  NSDate+Helper.m
//  NJUSTHelper
//
//  Created by 郑越辉 on 2017/9/22.
//  Copyright © 2017年 njust. All rights reserved.
//

#import "NSDate+Helper.h"
#import "NSString+Helper.h"
@implementation NSDate (Helper)
-(NSDate *) dayAfterDays:(NSInteger) days {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:0];
    [adcomps setDay:days];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:self options:0];
    return newdate;
}
-(NSDate *) dayBeforeDays:(NSInteger) days {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:0];
    [adcomps setDay:0-days];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:self options:0];
    return newdate;
}

-(NSInteger) getWeekIndex {
    return [NSString indexOfWeekString:[NSString weekStringWithDate:self]];
}
-(NSDate *)getMondayThisWeek{
    return [self dayAfterDays:0-[self getWeekIndex]];
}
+(NSDate *) getTodayNoonDate{
    NSDate *now = [NSDate date];
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ 12:00", [NSString yearStringWithDate:now], [NSString monthStringWithDate:now], [NSString dayStringWithDate:now]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
    NSDate *today = [dateFormatter dateFromString:dateStr];
    return today;
}
+(NSDate *) getFirstDay {
    NSDate *now = [NSDate date];
    NSDate *firstDay = nil;
    
    int month = [[NSString monthStringWithDate:now] intValue];
    if(month >= 8) {
        ///上学期,起始时间为9月1号之前的周一
        NSString *startWeekDayStr= [NSString stringWithFormat:@"%@-09-01 06:00", [NSString yearStringWithDate:now]];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
        NSDate *startWeekDay = [dateFormatter dateFromString:startWeekDayStr];
        int weekIndex = [NSString indexOfWeekString:[NSString weekStringWithDate:startWeekDay]];
        firstDay = [startWeekDay dayBeforeDays:weekIndex];
    }else {
        
    }
    return firstDay;
}

-(NSInteger) compsEraToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitEra fromDate:self toDate:date options:0];
    return [comps era];
}
-(NSInteger) compsYearToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitYear fromDate:self toDate:date options:0];
    return [comps year];
}
-(NSInteger) compsMonthToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitMonth fromDate:self toDate:date options:0];
    return [comps month];
}
-(NSInteger) compsWeekToDate:(NSDate *)date {
    ///desc = YES : self时间在date之前
    ///desc = NO  : self时间在date之后
    BOOL desc = [self compsSecondToDate:date] > 0;
    NSInteger days = [self compsDayToDate:date];
    NSInteger weeks = days / 7;
    if(([self getWeekIndex] > [date getWeekIndex]) && desc){
        weeks = weeks + 1;
    }
    if(([self getWeekIndex] < [date getWeekIndex]) && !desc){
        weeks = weeks - 1;
    }
    
    return weeks;
}
-(NSInteger) compsDayToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitDay fromDate:self toDate:date options:0];
    return [comps day];
}
-(NSInteger) compsHourToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitHour fromDate:self toDate:date options:0];
    return [comps hour];
}
-(NSInteger) compsMinuteToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitMinute fromDate:self toDate:date options:0];
    return [comps hour];
}
-(NSInteger) compsSecondToDate:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitSecond fromDate:self toDate:date options:0];
    return [comps hour];
}




@end
